﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server.ActivityFeed;

namespace ESF.SharePoint.Utility
{
    class SocialActivityHelper
    {
        public static ActivityType GetActivityType(ActivityManager am, string applicationName, string activityTypeName)
        {
            // Create an activity application.
            //string applicationName = "EnhancedSocialTools";
            ActivityApplication app = am.ActivityApplications[applicationName];

            //Create an ActivityType; check to see whether it already exists.
            //string activityTypeName = "";
            return app.ActivityTypes[activityTypeName];
        }

        public static ActivityType GetActivityType(ActivityManager am, string applicationName, long activityTypeId)
        {
            // Create an activity application.
            //string applicationName = "EnhancedSocialTools";
            ActivityApplication app = am.ActivityApplications[applicationName];

            //Create an ActivityType; check to see whether it already exists.
            //string activityTypeName = "";
            return app.ActivityTypes[activityTypeId];
        }
        
        public void SetupCustomActivity(SPSite site, string applicationName, string activityTypeName)
        {

            string resFile = "name of your resource (.resx) file";
            //Get an SPServiceContext from the site.
            SPServiceContext context = SPServiceContext.GetContext(site);
            //Create a UserProfileManager.
            UserProfileManager upm = new UserProfileManager(context);
            //Get the current user's profile.
            UserProfile p = upm.GetUserProfile(true);

            //Create an activity manager.
            ActivityManager am = new ActivityManager(p, context);
            //Make sure that the current user has permission to do this
            bool hasrights = am.PrepareToAllowSchemaChanges();

            // Create an activity application.
            //string applicationName = "EnhancedSocialTools";
            ActivityApplication app = am.ActivityApplications[applicationName];
            if (app == null)
            {
                app = am.ActivityApplications.Create(applicationName);
                app.Commit();
            }

            //Create an ActivityType; check to see whether it already exists.
            //string activityTypeName = "";
            ActivityType activityType = app.ActivityTypes[activityTypeName];
            if (activityType == null)
            {
                activityType = app.ActivityTypes.Create(activityTypeName);
                //Associate the ActivityType with the resource file that you have deployed and with the
                //localized string name stored in the resource file.
                activityType.ActivityTypeNameLocStringResourceFile = resFile;
                activityType.ActivityTypeNameLocStringName = "Activity Type Localized String Name";
                activityType.IsPublished = true;
                activityType.IsConsolidated = true;
                activityType.Commit();
            }

            //Create a single value ActivityTemplate; check to see whether it already exists.
            ActivityTemplate urlTemplateSV = activityType.ActivityTemplates[ActivityTemplatesCollection.CreateKey(false)];
            if (urlTemplateSV == null)
            {
                urlTemplateSV = activityType.ActivityTemplates.Create(false);
                //Associate the template with the resource file that you have deployed and with the
                //localized string name stored in the resource file.
                  urlTemplateSV.TitleFormatLocStringResourceFile = resFile;
                  urlTemplateSV.TitleFormatLocStringName = "Activity Template Localized String Name ";
                  urlTemplateSV.Commit();
            }
        }

    }
}
